//
//  HotChatTableViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/18.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "HotChatTableViewController.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "AFHTTPRequestOperationManager.h"
#import "NoImageTableViewCell.h"
#import "BigImageTableViewCell.h"
#import "NomalModel.h"
#define NoImageCell @"cell1"
#define ImageCell @"cell"
#import "DetailViewController.h"
#import "HomeModel.h"

@interface HotChatTableViewController()

@property (nonatomic,copy) NSString *myUrl;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isRefresh;
@property (nonatomic,assign) NSInteger pageNum;

@end

@implementation HotChatTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.pageNum = 1;
    
    self.dataArray = [NSMutableArray array];
    
    [self.tableView registerClass:[BigImageTableViewCell class] forCellReuseIdentifier:ImageCell];
    [self.tableView registerClass:[NoImageTableViewCell class] forCellReuseIdentifier:NoImageCell];
    self.myUrl = @"http://api.m.jiemian.com/article/cate/181.json";
    [self data];
    // 调用刷新
    [self setupRefresh];
 
    
    
    
}

// 刷新
-(void)setupRefresh {
    //下拉刷新
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
    [self.tableView headerBeginRefreshing];
    
    
    //上拉加载跟多(进入刷新状态就会调用self的footerRefreshing)
    
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    

    
}

// 下拉方法
-(void)headerRereshing {
    //[self checkNetWork];
    _pageNum = 1;
    self.isRefresh = NO;
    [self data];
    [self.tableView headerEndRefreshing];
    
    
}

// 上拉方法
-(void)footerRereshing {
    _pageNum ++;
    
    self.isRefresh = YES;
    [self data];
    
    [self.tableView footerEndRefreshing];
    
    
}

-(void)data{
    // 网络请求
    AFHTTPRequestOperationManager * manager= [AFHTTPRequestOperationManager manager];
    NSString *url = [self.myUrl stringByAppendingString:[NSString stringWithFormat:@"?&page=%ld",self.pageNum]];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (!self.isRefresh) {
            [self.dataArray removeAllObjects];
        }
        NSDictionary *dic = responseObject[@"result"];
        NSArray *array1 =dic[@"rst"];
        for (NSDictionary *dic1 in array1) {
            NomalModel * normal = [NomalModel new];
            normal.title = dic1[@"title"];
            normal.MyID = dic1[@"id"];
            normal.publishTime = dic1[@"publishtime"];
            normal.comment = dic1[@"comment"];
            normal.photo = dic1[@"l_image"];
            [self.dataArray addObject:normal];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    [self.tableView reloadData];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    //NSLog(@"%ld",self.dataArray.count);
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NomalModel * normal = _dataArray[indexPath.row];
    if (normal.photo.length==0) {
        NoImageTableViewCell * noImageCell = [tableView dequeueReusableCellWithIdentifier:NoImageCell forIndexPath:indexPath];
        noImageCell.commentLabel.text = [NSString stringWithFormat:@"评论(%@)", normal.comment];
        noImageCell.titleLabel.text = normal.title;
        noImageCell.timeLabel.text =  [self timeFormatted:[normal.publishTime integerValue]];
        return noImageCell;
    }else{
        
        BigImageTableViewCell * bigImageCell = [tableView dequeueReusableCellWithIdentifier:ImageCell forIndexPath:indexPath];
        bigImageCell.titleLabel.text = normal.title;
        bigImageCell.timeLabel.text =  [self timeFormatted:[normal.publishTime integerValue]];
        bigImageCell.commentLabel.text = [NSString stringWithFormat:@"评论(%@)", normal.comment];
        NSURL *url = [NSURL URLWithString:normal.photo];
        [bigImageCell.picImageView sd_setImageWithURL:url];

        return bigImageCell;
        
    }
    
}

// 将秒数转化成日期
- (NSString *)timeFormatted:(long long)totalSeconds
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:totalSeconds];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM/dd HH:mm:ss"];
    NSString *showtimeNew = [formatter stringFromDate:date];
    
    return showtimeNew;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NomalModel *normal= _dataArray[indexPath.row];
    if (normal.photo.length == 0) {
        return 35 + [NoImageTableViewCell stringHeight:normal.title];
    }else{
        return 275 + [NoImageTableViewCell stringHeight:normal.title ];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 刚选中又马上取消选中，格子不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    UIViewController *vc = [[UIViewController alloc]init];
    vc.view.backgroundColor = [UIColor yellowColor];
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    
    HomeModel *home = [[HomeModel alloc]init];
    
    home = self.dataArray[indexPath.row];
    
    detailVC.homeData = home;
    
    detailVC.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
